package com.gz.logistics.library;

import android.content.Context;

import com.gz.logistics.library.listener.BaseListener;
import com.gz.logistics.library.listener.CheckVersionListener;
import com.gz.logistics.library.listener.GetClothesBugListener;
import com.gz.logistics.library.listener.GetClothesCategoryListener;
import com.gz.logistics.library.listener.GetClothesColorListener;
import com.gz.logistics.library.listener.GetClothesGroupListener;
import com.gz.logistics.library.listener.GetClothesNameListener;
import com.gz.logistics.library.listener.GetClothesOrderListener;
import com.gz.logistics.library.listener.GetClothesPinpaiListener;
import com.gz.logistics.library.listener.GetClothesSpecialListener;
import com.gz.logistics.library.listener.GetCommunityBuildingListener;
import com.gz.logistics.library.listener.GetCommunityGroupListener;
import com.gz.logistics.library.listener.GetQueryorderDetailListener;
import com.gz.logistics.library.listener.GetStoreHouseListener;
import com.gz.logistics.library.listener.GetSumOfMoneyListener;
import com.gz.logistics.library.listener.PostClothesInfoListener;
import com.gz.logistics.library.listener.PostMoneyListener;
import com.gz.logistics.library.listener.PostSignListener;
import com.gz.logistics.library.util.Constants;
import com.gz.logistics.library.util.CrashHandler;
import com.gz.logistics.library.util.PreferenceModel;
import com.gz.logistics.library.util.SingtonAsyncHttpClient;
import com.gz.logistics.library.util.Tools;
import com.loopj.android.http.RequestParams;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 订单相关的接口
 * author wanner1991
 */
public class IOrder {
    private static final String TAG = IOrder.class.getSimpleName();
    private static  IOrder mInstance;
    private static Lock mLocal;
    private Context mContext;

    static {
        mLocal = new ReentrantLock();
    }

    private IOrder(Context mContext) {
        this.mContext = mContext;
    }

    public static IOrder getInstance(){
        return mInstance;
    }

    public static  void init(Context mContext){
        if (mInstance == null){
            mLocal.lock();
            try {
                if (mInstance == null){
                    mInstance = new IOrder(mContext);
                }
            } finally {
                mLocal.unlock();
            }

        }
    }

    /**
     * 获取主页订单列表接口
     * @param transStatus 物流签收状态
     * @param signStatus  业务签收状态
     * @param Listener 回调接口
     */
    public void getHomeOrder(int transStatus , int signStatus , BaseListener Listener){
        if (!Tools.hasInternetConnection(mContext)){
            Listener.onNoNetWork();
        }else{
            RequestParams params = new RequestParams();
            params.put("user_id", PreferenceModel.getInt(Constants.PREFERENCE_USER_ID,-1));
            params.put("trans_status",transStatus);
            params.put("sign_status",signStatus);
            SingtonAsyncHttpClient.getInstance().get(Constants.URL_GET_HOME_ORDERS, params, Listener);
        }

    }

    /**
     * 单订单物流签收
     * @param sn 订单编号
     *
     * @param mListener 回调接口
     */
    public void singalorderLogisticsStatusSign(String sn,BaseListener mListener){
        if (!Tools.hasInternetConnection(mContext)){
            mListener.onNoNetWork();
        }else{
            RequestParams params = new RequestParams();
            params.put("user_id", PreferenceModel.getInt(Constants.PREFERENCE_USER_ID,-1));
            params.put("sn",sn);
            SingtonAsyncHttpClient.getInstance().get(Constants.URL_SINGLE_ORDER_LOGISTICS_SIGN, params, mListener);
        }
    }
    /**
     * 单订单业务签收
     * @param sn 订单编号
     *
     * @param mListener 回调接口
     */
    public void singalorderDeliveryStatusSign(String sn,int paytype,String realmoney,String notes,int bank_flag,String pos_code,String bank_no,String batch_no,BaseListener mListener){
        if (!Tools.hasInternetConnection(mContext)){
            mListener.onNoNetWork();
        }else{
            RequestParams params = new RequestParams();
            params.put("sn",sn);
            params.put("user_id", PreferenceModel.getInt(Constants.PREFERENCE_USER_ID,-1));
            params.put("payType",paytype);
            params.put("real_money",realmoney);
            params.put("notes",notes);
            params.put("bank_flag",bank_flag);
            params.put("pos_code",pos_code);
            params.put("bank_no",bank_no);
            params.put("batch_no",batch_no);
            SingtonAsyncHttpClient.getInstance().get(Constants.URL_SINGLE_ORDER_DELIVERY_SIGN, params, mListener);
            CrashHandler.e(TAG,Constants.URL_SINGLE_ORDER_DELIVERY_SIGN+"?"+params+"=================");
        }
    }

    /**
     * 批量进行物流签收
     * @param sns 订单编号，以逗号隔开
     * @param listener
     */
    public void batchOrdersLogisticsStatusSign(String sns ,BaseListener listener){
        if (!Tools.hasInternetConnection(mContext)){
            listener.onNoNetWork();
        }else{
            RequestParams params = new RequestParams();
            params.put("sns",sns);
            params.put("user_id",PreferenceModel.getInt(Constants.PREFERENCE_USER_ID,-1));
            SingtonAsyncHttpClient.getInstance().post(Constants.URL_BATCH_ORDER_LOGISTICS_SIGN, params, listener);
        }

    }
    /**
     * 批量进行业务签收
     * @param sns 订单编号，以逗号隔开
     * @param listener
     */
    public void batchOrdersDeliveryStatusSign(String sns ,BaseListener listener){
        if (!Tools.hasInternetConnection(mContext)){
            listener.onNoNetWork();
        }else{
            RequestParams params = new RequestParams();
            params.put("sns",sns);
            params.put("user_id",PreferenceModel.getInt(Constants.PREFERENCE_USER_ID,-1));
            SingtonAsyncHttpClient.getInstance().post(Constants.URL_BATCH_ORDER_DELIVERY_SIGN, params, listener);
        }

    }

    /**
     * 获取片区列表
     * @param listener
     */
    public void getParts(BaseListener listener){
        if (!Tools.hasInternetConnection(mContext)){
            listener.onNoNetWork();
        }else{
            RequestParams params = new RequestParams();
            params.put("user_id", PreferenceModel.getInt(Constants.PREFERENCE_USER_ID, -1));
            SingtonAsyncHttpClient.getInstance().get(Constants.URL_GET_PARTS, params, listener);
        }

    }

    /**
     * 获取预出库单列表
     */
    public void getWillOutStorageAssemblesW(int storehouse_id,String begin_time ,String end_time , int sign_status,BaseListener listener){
        if (!Tools.hasInternetConnection(mContext)){
            listener.onNoNetWork();
        }else{
            RequestParams params = new RequestParams();
            if (storehouse_id > 0 ){
                params.put("storehouse_id",storehouse_id);
            }
            if (begin_time != null){
                params.put("begin_at",begin_time);
            }
            if (end_time != null){
                params.put("end_at",end_time);
            }
            if (sign_status > 0){
                params.put("sign_status",sign_status);
            }
            params.put("user_id", PreferenceModel.getInt(Constants.PREFERENCE_USER_ID, -1));
            SingtonAsyncHttpClient.getInstance().get(Constants.URL_GET_Will_OUT_STORAGE, params, listener);
        }
    }
    /**
     * 获取预出库单列表
     */
    public void getWillOutStorageAssemblesY(int storehouse_id,String begin_time ,String end_time , int sign_status,int page,BaseListener listener){
        if (!Tools.hasInternetConnection(mContext)){
            listener.onNoNetWork();
        }else{
            RequestParams params = new RequestParams();
            if (storehouse_id > 0 ){
                params.put("storehouse_id",storehouse_id);
            }
            if (begin_time != null){
                params.put("begin_at",begin_time);
            }
            if (end_time != null){
                params.put("end_at",end_time);
            }
            if (sign_status > 0){
                params.put("sign_status",sign_status);
            }
            params.put("page",page);
            params.put("user_id", PreferenceModel.getInt(Constants.PREFERENCE_USER_ID, -1));
            SingtonAsyncHttpClient.getInstance().get(Constants.URL_GET_WILL_OUT_YES, params, listener);
        }
    }

    /**
     * 获取预出库单详情接口
     * @param homeOrderListener
     */
    public void getAssembleDetail(int storehouse_id,String begin_at,String end_at,int sign_status, BaseListener homeOrderListener){
        if (!Tools.hasInternetConnection(mContext)){
            homeOrderListener.onNoNetWork();
        }else{
            RequestParams params = new RequestParams();
            params.put("user_id", PreferenceModel.getInt(Constants.PREFERENCE_USER_ID, -1));
            params.put("storehouse_id", storehouse_id);
            params.put("begin_at", begin_at);
            params.put("end_at", end_at);
            params.put("sign_status", sign_status);
            SingtonAsyncHttpClient.getInstance().get(Constants.URL_GET_WILL_OUT_ASSEMBLE_DETAIL, params, homeOrderListener);
        }

    }

    /**
     * 签收某个预出库单下的所有订单
     * @param storage_out_id 预出库单的id
     * @param listener
     */
    public void signAllOrdersOfThisAssemble(int storage_out_id ,String begin_at,String end_at,BaseListener listener){
        if (!Tools.hasInternetConnection(mContext)){
            listener.onNoNetWork();
        }else{
            RequestParams params = new RequestParams();
            params.put("user_id", PreferenceModel.getInt(Constants.PREFERENCE_USER_ID, -1));
            params.put("storehouse_id", storage_out_id);
            params.put("begin_at",begin_at);
            params.put("end_at",end_at);
            SingtonAsyncHttpClient.getInstance().get(Constants.URL_BATCH_ORDER_LOGISTICS_SIGN, params, listener);
        }

    }

    /**
     *  未签收订单查询页面，订单列表获取 接口
     * @param Listener
     */
    public void getQueryOrder(String storehouse_id,String group_id,String building_id,String begin_at,String end_at,String sign_status,BaseListener Listener){
        if (!Tools.hasInternetConnection(mContext)){
            Listener.onNoNetWork();
        }else{

            RequestParams params = new RequestParams();

            params.put("user_id", PreferenceModel.getInt(Constants.PREFERENCE_USER_ID,-1));
            if (!storehouse_id.equals("0")){
                params.put("storehouse_id",storehouse_id);
            }
            if (!group_id.equals("0")){
                params.put("group_id",group_id);
            }
            if (!building_id.equals("0")){
                params.put("building_id",building_id);
            }
            if (begin_at != null){
                params.put("begin_at",begin_at);
            }
            if (end_at != null){
                params.put("end_at",end_at);
            }
            params.put("sign_status", sign_status);
            SingtonAsyncHttpClient.getInstance().get(Constants.URL_GET_ORDER_STATUS_2, params, Listener);
            CrashHandler.d(TAG, Constants.URL_GET_ORDER_STATUS_2 + params);
        }

    }
    /**
     *  已签收订单查询页面，订单列表获取 接口
     * @param Listener
     */
    public void getQueryOrderY(String storehouse_id,String group_id,String building_id,String begin_at,String end_at,String sign_status,int page,BaseListener Listener){
        if (!Tools.hasInternetConnection(mContext)){
            Listener.onNoNetWork();
        }else{

            RequestParams params = new RequestParams();
            params.put("page",page);
            params.put("user_id", PreferenceModel.getInt(Constants.PREFERENCE_USER_ID,-1));
            if (!storehouse_id.equals("0")){
                params.put("storehouse_id",storehouse_id);
            }
            if (!group_id.equals("0")){
                params.put("group_id",group_id);
            }
            if (!building_id.equals("0")){
                params.put("building_id",building_id);
            }
            if (begin_at != null){
                params.put("begin_at",begin_at);
            }
            if (end_at != null){
                params.put("end_at",end_at);
            }
            params.put("sign_status", sign_status);
            SingtonAsyncHttpClient.getInstance().get(Constants.URL_GET_HOME_ORDERS, params, Listener);
        }

    }
    /**
     * 获取二维码扫描出的订单详情
     * @param sn 订单编号
     *
     * @param mListener 回调接口
     */
    public void getQRCodeOrderDetail(String sn,BaseListener mListener){
        if (!Tools.hasInternetConnection(mContext)){
            mListener.onNoNetWork();
        }else{
            RequestParams params = new RequestParams();
            params.put("user_id", PreferenceModel.getInt(Constants.PREFERENCE_USER_ID,-1));
            params.put("sn", sn);
            SingtonAsyncHttpClient.getInstance().get(Constants.URL_GET_QRCODE_ORDER_DETAIL,params,mListener);
        }
    }

    /**
     * 根据登陆用户的id查询仓库列表
     * @param user_id 当前登录用户的id
     * 杜志娟 2016-03-11
     */
    public void getStoreHouse(String user_id,GetStoreHouseListener storelistener){
        if (!Tools.hasInternetConnection(mContext)){
            storelistener.onNoNetWork();
        }else{
            RequestParams params = new RequestParams();
            params.put("user_id",user_id);
            SingtonAsyncHttpClient.getInstance().get(Constants.URL_GET_STORE_HOUSE,params,storelistener);
        }

    }

    /**
     * 根据仓库的id查询小区
     *@param storehouse_id 仓库的id
     *  杜志娟 2016-03-11
     */
    public void getCommunityGroup(String storehouse_id,GetCommunityGroupListener groupListener){
        if (!Tools.hasInternetConnection(mContext)){
            groupListener.onNoNetWork();
        }else{
            RequestParams params = new RequestParams();
            params.put("storehouse_id",storehouse_id);
            SingtonAsyncHttpClient.getInstance().get(Constants.URL_GET_COMMUNITY,params,groupListener);
        }


    }

    /**
     * 根据仓库的id查询小区
     *@param group_id 仓库的id
     *  杜志娟 2016-03-11
     */
    public void getBuildings(String group_id,GetCommunityBuildingListener groupListener){
        if (!Tools.hasInternetConnection(mContext)){
            groupListener.onNoNetWork();
        }else{
            RequestParams params = new RequestParams();
            params.put("group_id",group_id);
            SingtonAsyncHttpClient.getInstance().get(Constants.URL_GET_BUILDINGS,params,groupListener);
        }

    }
    /**
     * 获取订单详情
     *  杜志娟 2016-03-18
     */
    public void getOrderDetail(String sn,GetQueryorderDetailListener getQueryorderDetailListener){
        if (!Tools.hasInternetConnection(mContext)){
            getQueryorderDetailListener.onNoNetWork();
        }else{
            RequestParams params = new RequestParams();
            params.put("user_id", PreferenceModel.getInt(Constants.PREFERENCE_USER_ID,-1));
            params.put("sn",sn);
            SingtonAsyncHttpClient.getInstance().get(Constants.URL_GET_ORDER_DETAIL,params,getQueryorderDetailListener);
            CrashHandler.i(TAG, Constants.URL_GET_ORDER_DETAIL + "?" + params);
        }

    }
    /**
     * 获取金额汇总
     *  杜志娟 2016-03-21
     */
    public void setGetSumOfMoney(String storehouse_id,String begin_at,String end_at,GetSumOfMoneyListener getSumOfMoneyListener){
        if (!Tools.hasInternetConnection(mContext)){
            getSumOfMoneyListener.onNoNetWork();
        }else{
            RequestParams params = new RequestParams();
            params.put("user_id", PreferenceModel.getInt(Constants.PREFERENCE_USER_ID,-1));
            if (!storehouse_id.equals("0")){
                params.put("storehouse_id",storehouse_id);
            }
            if (begin_at!=null){
                params.put("begin_at",begin_at);
            }
            if (end_at!=null){
                params.put("end_at",end_at);
            }
            SingtonAsyncHttpClient.getInstance().get(Constants.URL_GET_DAY_MONEY,params,getSumOfMoneyListener);
        }

    }

    /**
     * 获取送衣取衣的小区信息
     * @param user_type
     * @param getClothesGroupListener
     */
    public void getClothesCommunity(int user_type,GetClothesGroupListener getClothesGroupListener){
        if (!Tools.hasInternetConnection(mContext)){
            getClothesGroupListener.onNoNetWork();
        }else{
            RequestParams params = new RequestParams();
            params.put("user_id", PreferenceModel.getInt(Constants.PREFERENCE_USER_ID,-1));
            params.put("user_type",user_type);
            SingtonAsyncHttpClient.getInstance().get(Constants.URL_GET_CLOTHES_COMMUNITYS, params, getClothesGroupListener);
            CrashHandler.i(TAG, Constants.URL_GET_CLOTHES_COMMUNITYS + "?" + params);
        }

    }
    /**
     * 获取送衣取衣的楼房信息
     * @param user_type
     * @param getClothesGroupListener
     */
    public void getClothesBuildings(int group_id,int user_type,GetClothesGroupListener getClothesGroupListener){
        if (!Tools.hasInternetConnection(mContext)){
            getClothesGroupListener.onNoNetWork();
        }else{
            RequestParams params = new RequestParams();
            params.put("user_type",user_type);
            params.put("group_id",group_id);
            SingtonAsyncHttpClient.getInstance().get(Constants.URL_GET_CLOTHES_BUILDINGS, params, getClothesGroupListener);
            CrashHandler.i(TAG,Constants.URL_GET_CLOTHES_BUILDINGS+"?"+params);
        }

    }

    /**
     * 获取送衣取衣的楼房信息
     */
    public void getClothesOrders(int group_id,int building_id,int status,int user_type,int page,String begin_at,String end_at,GetClothesOrderListener getClothesOrderListener){
        if (!Tools.hasInternetConnection(mContext)){
            getClothesOrderListener.onNoNetWork();
        }else{
            RequestParams params = new RequestParams();
            params.put("user_id", PreferenceModel.getInt(Constants.PREFERENCE_USER_ID,-1));
            params.put("group_id",group_id);
            params.put("building_id",building_id);
            params.put("status",status);
            params.put("begin_at",begin_at);
            params.put("end_at",end_at);
            params.put("user_type",user_type);
            params.put("page",page);
            SingtonAsyncHttpClient.getInstance().get(Constants.URL_GET_CLOTHES_ORDERS, params, getClothesOrderListener);
            CrashHandler.i(TAG,Constants.URL_GET_CLOTHES_ORDERS+"?"+params);
        }
    }

    public void getClothesCategory(GetClothesCategoryListener getClothesCategoryListener){
        if (!Tools.hasInternetConnection(mContext)){
            getClothesCategoryListener.onNoNetWork();
        }else{
            SingtonAsyncHttpClient.getInstance().get(Constants.URL_GET_CKOTHES_CATEGORY, getClothesCategoryListener);
            CrashHandler.i(TAG, Constants.URL_GET_CKOTHES_CATEGORY);
        }

    }
    public void getClothesName(int clothing_category_id,GetClothesNameListener getClothesNameListener){
        if (!Tools.hasInternetConnection(mContext)){
            getClothesNameListener.onNoNetWork();
        }else{
            RequestParams params = new RequestParams();
            params.put("clothing_category_id",clothing_category_id);
            SingtonAsyncHttpClient.getInstance().get(Constants.URL_GET_CLOTHES_NAME,params, getClothesNameListener);
            CrashHandler.i(TAG, Constants.URL_GET_CLOTHES_NAME+"?"+params);
        }
    }
    public void getClothesColor(GetClothesColorListener getClothesColorListener){
        if (!Tools.hasInternetConnection(mContext)){
            getClothesColorListener.onNoNetWork();
        }else{
            SingtonAsyncHttpClient.getInstance().get(Constants.URL_GET_CLOTHES_COLOR, getClothesColorListener);
            CrashHandler.i(TAG, Constants.URL_GET_CLOTHES_COLOR);
        }

    }
    public void getClothesPinpai(GetClothesPinpaiListener getClothesPinpaiListener){
        if (!Tools.hasInternetConnection(mContext)){
            getClothesPinpaiListener.onNoNetWork();
        }else{
            SingtonAsyncHttpClient.getInstance().get(Constants.URL_GET_CLOTHES_PINPAI, getClothesPinpaiListener);
            CrashHandler.i(TAG, Constants.URL_GET_CLOTHES_PINPAI);
        }

    }
    public void getClothesBugs(GetClothesBugListener getClothesBugListener){
        if (!Tools.hasInternetConnection(mContext)){
            getClothesBugListener.onNoNetWork();
        }else{
            SingtonAsyncHttpClient.getInstance().get(Constants.URL_GET_CLOTHES_BUGS, getClothesBugListener);
            CrashHandler.i(TAG, Constants.URL_GET_CLOTHES_BUGS);
        }

    }
    public void getClothesSpecial(GetClothesSpecialListener getClothesSpecialListener){
        if (!Tools.hasInternetConnection(mContext)){
            getClothesSpecialListener.onNoNetWork();
        }else{
            SingtonAsyncHttpClient.getInstance().get(Constants.URL_GET_CLOTHES_SPECIAL, getClothesSpecialListener);
            CrashHandler.i(TAG, Constants.URL_GET_CLOTHES_SPECIAL);
        }

    }

    /**
     * 提交衣物信息
     * @param category
     * @param price
     * @param name_id
     * @param color
     * @param pinpai
     * @param bug
     * @param special
     * @param order_num
     * @param order_no
     * @param yuyue_time
     * @param money
     * @param postClothesInfoListener
     */
    public void PostClothesInfo(int category,double price,int name_id,String color,String pinpai,String bug,String special,String order_num,String order_no,String yuyue_time,double money,PostClothesInfoListener postClothesInfoListener){
        if (!Tools.hasInternetConnection(mContext)){
            postClothesInfoListener.onNoNetWork();
        }else{
            RequestParams params = new RequestParams();
            params.put("clothing_category_id",category);
            params.put("money",price);
            params.put("price_id",name_id);
            params.put("colour",color);
            params.put("brand",pinpai);
            params.put("flaws_note",bug);
            params.put("special_process",special);
            params.put("label_no",order_num);
            params.put("order_no",order_no);
            params.put("favorite_delivery_date",yuyue_time);
            params.put("additional_money",money);
            params.put("user_id",PreferenceModel.getInt(Constants.PREFERENCE_USER_ID,-1));
            SingtonAsyncHttpClient.getInstance().post(Constants.URL_POST_CLOTHES_INFO, params, postClothesInfoListener);
            CrashHandler.i(TAG, Constants.URL_POST_CLOTHES_INFO+"?"+params);
        }

    }

    /**
     * 收款
     */
    public void PostReceipt(int user_type,String order_no,String money,PostMoneyListener postMoneyListener){
        if (!Tools.hasInternetConnection(mContext)){
            postMoneyListener.onNoNetWork();
        }else{
            RequestParams params = new RequestParams();
            params.put("user_type",user_type);
            params.put("order_no",order_no);
            params.put("money",money);
            SingtonAsyncHttpClient.getInstance().post(Constants.URL_POST_CLOTHES_MONEY, params, postMoneyListener);
            CrashHandler.i(TAG, Constants.URL_POST_CLOTHES_MONEY+"?"+params);
        }
    }
    /**
     * 签收
     */
    public void PostSign(String order_no,PostSignListener postSignListener){
        if (!Tools.hasInternetConnection(mContext)){
            postSignListener.onNoNetWork();
        }else{
            RequestParams params = new RequestParams();
            params.put("order_no",order_no);
            params.put("user_id",PreferenceModel.getInt(Constants.PREFERENCE_USER_ID,-1));
            SingtonAsyncHttpClient.getInstance().post(Constants.URL_POST_CLOTHES_SIGN, params, postSignListener);
            CrashHandler.i(TAG, Constants.URL_POST_CLOTHES_SIGN+"?"+params);
        }
    }

    /**
     * 检查程序版本号
     * @param context 上下文对象
     * @param packageSuffix 包名最后一段
     * @param checkVersionListener 回调监听器
     */
    public static void checkVersion(Context context, String packageSuffix, CheckVersionListener checkVersionListener) {
        if(!Tools.hasInternetConnection(context)) {
            checkVersionListener.onNoNetWork();

        }else {
            String versionName = Tools.getAppVersionName(context);
            RequestParams params = new RequestParams();
            params.put("channel", packageSuffix);

            params.put("appver", versionName);
//			params.put("packname", mContext.getPackageName());
            checkVersionListener.versionName = versionName;
            SingtonAsyncHttpClient.getInstance().get(Constants.URL_CHECK_VERSION, params, checkVersionListener);
            CrashHandler.d(TAG, "url = " + Constants.URL_CHECK_VERSION + "?" + params);
        }
    }
}
